﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Windows.Forms;
using System.Globalization;

// MobiLib, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace MobiLibrary
{
    public class LanguageCodes
    {
        //
        //==================================================================================================================================
        //
        public struct Lang
        {
            public int Lcid;
            public string Name;

            public Lang(CultureInfo ci)
            {
                this.Lcid = ci.LCID;
                this.Name = ci.DisplayName;
            }
            public override string ToString()
            {
                return this.Name;
            }
        }
        private static CultureInfo[] cultures = null;
        static LanguageCodes()
        {
            cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
            //DisplayName,EnglishName,Name,NativeName, IetfLanguageTag,ThreeLetterISOLanguageName,ThreeLetterWindowsLanguageName,TwoLetterISOLanguageName, LCID
        }

        public static void PrintCulture(CultureInfo culture)
        {
                Console.WriteLine("[{0}], {3}, {4}, {5}, {6}, {7}", culture.DisplayName, culture.EnglishName, culture.NativeName, culture.IetfLanguageTag, culture.ThreeLetterISOLanguageName, culture.ThreeLetterWindowsLanguageName, culture.TwoLetterISOLanguageName, culture.LCID);
        }

        public static int CurrentLCID()
        {
            return InputLanguage.CurrentInputLanguage.Culture.LCID;
        }

        // dla 1034 sp-sp nie ma na liście culture, ale GetCulture rozpoznaje go poprawnie. Dziwne????
        // O to chodzi...
        //Spanish - Spain (Modern Sort) 0c0a 3082
        //Spanish - Spain (Traditional Sort) 040a 1034

        public static int RecodeLcid(int lcid)
        {
            try
            {
                return CultureInfo.GetCultureInfo(CultureInfo.GetCultureInfo(lcid).Name).LCID;
            }
            catch
            {
                return 0;
            }
        }

        public static string ToText(int lcid)
        {
            try
            {
                CultureInfo ci = new CultureInfo(lcid);
                return string.Format("{0} ({1}/0x{2:X})", ci.Name, ci.DisplayName, lcid);
            }
            catch
            {
                return string.Format("Unknown (0x{0:X})", lcid);
            }
        }
        //
        public static string ToTextShort(int lcid)
        {
            try
            {
                CultureInfo ci = new CultureInfo(lcid);
                return ci.Name;
            }
            catch
            {
                return lcid.ToString();
            }
        }
        //
        public static List<Lang> GetLangs()
        {
            List<Lang> l = new List<Lang>();
            l.Add(new Lang() {Lcid=0, Name="Unknown" });
            foreach (CultureInfo ci in cultures)
            {
                if (127 == ci.LCID) continue;
                if (1279 == ci.LCID) continue;//HID (Human Interface Device)04ff 1279
                l.Add(new Lang(ci));
            }
            return l;
        }
        //
        //
        //
        //
        //

        public static bool IsLCIDOK(int lcid)
        {
            try
            {
                CultureInfo ci = new CultureInfo(lcid);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static void ShowAll()
        {
            foreach (CultureInfo culture in cultures)
            {
                if (127 == culture.LCID) continue;
                PrintCulture(culture);
            }
        }

        /// <summary>
        /// Converts name of langage, or its numeric value into LCID, it uses Exception
        /// </summary>
        /// <param name="name">Name of language</param>
        /// <returns>LCID, 0 if parameter is invalid</returns>
        public static int Name2LCID(string name)
        {
            int lcid = 0;
            name = name.Trim();
            //
            try
            {
                if (name.StartsWith("0x")) lcid = Convert.ToInt32(name,16);
                else if (name.StartsWith("0X")) lcid = Convert.ToInt32(name, 16);
                else if (name.StartsWith("0")) lcid = Convert.ToInt32(name, 8);
                else lcid = Convert.ToInt32(name);
                if (IsLCIDOK(lcid)) return lcid;
            }
            catch
            {
                lcid = 0;
            }
            //return lcid;
            //int iTmp = 0;
            //Convert.ToInt32(name);

            //if (int.TryParse(name, out iTmp))
            //{
                //if (IsLCIDOK(iTmp)) return iTmp;
                //return 0;
            //}
            //
            foreach (CultureInfo culture in cultures)
            {
                if (0 == string.Compare(name, culture.Name, true)) { lcid = culture.LCID; break; }
                if (0 == string.Compare(name, culture.ThreeLetterISOLanguageName, true)) { lcid = culture.LCID; break; }
                if (0 == string.Compare(name, culture.ThreeLetterWindowsLanguageName, true)) { lcid = culture.LCID; break; }
                if (0 == string.Compare(name, culture.TwoLetterISOLanguageName, true)) { lcid = culture.LCID; break; }
                if (0 == string.Compare(name, culture.DisplayName, true)) { lcid = culture.LCID; break; }
                if (0 == string.Compare(name, culture.EnglishName, true)) { lcid = culture.LCID; break; }
                if (0 == string.Compare(name, culture.NativeName, true)) { lcid = culture.LCID; break; }
                //
                if (0 == string.Compare(name, culture.IetfLanguageTag, true)) { lcid = 0xFF & culture.LCID; break; }
            }
            return lcid;
        }
        //
        //==================================================================================================================================
        //
    }
}
